<template>
    <div class="modal-example">
      <!-- 页面主要内容 -->
      <div class="page-content">
        <h1>Vue3 弹窗示例</h1>
        <p>这是页面的主要内容，当弹窗显示时，这里的内容将无法被选中或点击。</p>
        <p>尝试选中这段文字，在弹窗未显示时是可以选中的。</p>
        <!-- 点击后更改showModal属性值 -->
        <button @click="showModal = true" class="open-modal-btn">
          打开弹窗
        </button>
      </div>
  
      <!-- 弹窗组件 -->
      <div v-if="showModal" class="modal-overlay" :class="{ 'active': showModal }">
        <div class="modal">
          <div class="modal-header">
            <h2>弹窗标题</h2>
            <button @click="showModal = false" class="close-btn">×</button>
          </div>
          <div class="modal-body">
            <p>这是弹窗的内容区域。</p>
            <p>此时底层内容无法被选中或点击。</p>
          </div>
          <div class="modal-footer">
            <button @click="showModal = false" class="close-modal-btn">
              关闭弹窗
            </button>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref } from 'vue';
  
  // 控制弹窗显示与隐藏的状态
  const showModal = ref(false);
  </script>
  
  <style scoped>
  .page-content {
    padding: 2rem;
    max-width: 800px;
    margin: 0 auto;
  }
  
  /* 打开弹窗按钮样式 */
  .open-modal-btn {
    padding: 0.8rem 1.5rem;
    background-color: #42b983;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
    transition: background-color 0.3s;
  }
  
  .open-modal-btn:hover {
    background-color: #359e75;
  }
  
  /* 弹窗遮罩层 */
  .modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
    /* 关键属性：阻止底层内容被选中 */
    pointer-events: none;
  }
  
  /* 弹窗显示时的状态 */
  .modal-overlay.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }
  
  /* 弹窗容器 */
  .modal {
    background-color: white;
    border-radius: 8px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    transform: translateY(-20px);
    transition: transform 0.3s;
  }
  
  .modal-overlay.active .modal {
    transform: translateY(0);
  }
  
  /* 弹窗头部 */
  .modal-header {
    padding: 1.2rem;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .modal-header h2 {
    margin: 0;
    font-size: 1.4rem;
  }
  
  /* 关闭按钮 */
  .close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #666;
    transition: color 0.2s;
  }
  
  .close-btn:hover {
    color: #333;
  }
  
  /* 弹窗内容区 */
  .modal-body {
    padding: 1.5rem;
  }
  
  /* 弹窗底部 */
  .modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid #e0e0e0;
    display: flex;
    justify-content: flex-end;
  }
  
  /* 关闭弹窗按钮 */
  .close-modal-btn {
    padding: 0.6rem 1.2rem;
    background-color: #f44336;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s;
  }
  
  .close-modal-btn:hover {
    background-color: #d32f2f;
  }
  </style>
  